﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ImageProcessing.Forms
{
    public enum SlideShowType { FromFolder, FromCurrentImages };
    public partial class SlideShowForm : Form
    {

        SlideShowType slidetype;
        SlideShowOptions options;
        SlideShowEffect effect;
        SlideShowSpeed speed;
        SlideShowTheme theme;
        SlideShowBackGround bg;
        private string[] folderFile = null;
        private int selected = 0;
        private int begin = 0;
        private int end = 0;
        int idle = 0;
        int x = 0, y = 0;

        public SlideShowForm(SlideShowType type)
        {
            InitializeComponent();
            slidetype = type;
            effect = SlideShowEffect.None;
            theme = SlideShowTheme.Classic;
            speed = SlideShowSpeed.Medium;
            bg = SlideShowBackGround.Default;
            this.WindowState = FormWindowState.Maximized;
            UpdatePcbLocation();

            if (slidetype == SlideShowType.FromFolder)
            {
                ChooseFolder();
            }
            else
            {
                this.Pcb_SlideShowOpen.Visible = false;
                this.panel3.Visible = false;
                showImage();
            }

            timer1.Enabled = true;
            timer1.Start();
            timer1.Interval = 5000;
            timer2.Enabled = true;
            timer2.Interval = 1000;
            timer2.Start();
        }

        private void ChooseFolder()
        {
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string[] part1 = null, part2 = null, part3 = null;
                part1 = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.jpg");
                part2 = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.jpeg");
                part3 = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.bmp");
                folderFile = new string[part1.Length + part2.Length + part3.Length];
                Array.Copy(part1, 0, folderFile, 0, part1.Length);
                Array.Copy(part2, 0, folderFile, part1.Length, part2.Length);
                Array.Copy(part3, 0, folderFile, part1.Length + part2.Length, part3.Length);
                selected = 0;
                begin = 0;
                end = folderFile.Length;
                // showImage(folderFile[selected]);
            }
        }

        private void showImage(string path)
        {
            Image imgtemp = Image.FromFile(path);
            this.UpdateDisplayedImage(imgtemp);
        }

        private void showImage()
        {
            Image imgtemp = Main.ImageList[Main.CurrImage].ImageBitmap;
            this.UpdateDisplayedImage(imgtemp);
        }

        private void UpdateDisplayedImage(Image imgtemp)
        {
            if (theme == SlideShowTheme.Classic)
            {
                ResizeARatio(imgtemp);
            }
            else
            {
                this.Fade(imgtemp);
            }
            if (effect == SlideShowEffect.Greyscale)
            {
                imgtemp = Operations.GreyScale((Bitmap)imgtemp);
            }
            else if (effect == SlideShowEffect.Sepia)
            {
                imgtemp = CalcOperation.SepiaEffect((Bitmap)imgtemp);
            }
            UpdatePcbLocation();
            pictureBox1.Image = imgtemp;

        }

        private void prevImage()
        {
            if (slidetype != SlideShowType.FromFolder)
            {
                if (Main.CurrImage == 0)
                {
                    Main.CurrImage = Main.ImageList.Count - 1;
                    showImage();
                }
                else
                {
                    Main.CurrImage--;
                    showImage();
                }
            }
            else
            {
                if (selected == 0)
                {
                    selected = folderFile.Length - 1;
                    showImage(folderFile[selected]);
                }
                else
                {
                    selected = selected - 1; showImage(folderFile[selected]);
                }
            }
        }

        private void nextImage()
        {
            if (slidetype == SlideShowType.FromFolder)
            {
                if (selected == folderFile.Length - 1)
                {
                    selected = 0;
                    showImage(folderFile[selected]);
                }
                else
                {
                    selected = selected + 1; showImage(folderFile[selected]);
                }
            }
            else
            {
                if (Main.CurrImage == Main.ImageList.Count - 1)
                {
                    Main.CurrImage = 0;
                    showImage();
                }
                else
                {
                    Main.CurrImage++;
                    showImage();
                }
            }
        }

        private void button4_Click(object sender, System.EventArgs e)
        {
            if (timer1.Enabled == true)
            {
                timer1.Enabled = false;
                timer1.Stop();
                Pcb_Pause.Image = Image.FromFile("Icons//PlayOver.png");
                Pcb_Pause.Name = "Pcb_Play";
            }
            else
            {
                timer1.Enabled = true;
                timer1.Start();
                Pcb_Pause.Image = Image.FromFile("Icons//PauseOver.png");
                Pcb_Pause.Name = "Pcb_Pause";
            }
        }

        private void timer1_Tick(object sender, System.EventArgs e)
        {
            nextImage();
        }

        private void ResizeARatio(Image img)
        {
            float factor;
            if (img.Width > this.Width && img.Height > (this.Height))
            {
                factor = (float)img.Width / this.Width > img.Height / (this.Height) ? (float)img.Width / this.Width : (float)img.Height / (this.Height);
            }
            else if (img.Width > this.Width)
            {
                factor = (float)img.Width / this.Width;
            }
            else if (img.Height > this.Height)
            {
                factor = (float)img.Height / (this.Height);
            }
            else
            {
                factor = 1;
            }
            factor = 1 / factor;
            this.pictureBox1.Height = (int)(img.Height * factor);
            this.pictureBox1.Width = (int)(img.Width * factor);
        }

        private void Fade(Image img)
        {
            float factor;
            if (img.Width > this.Width && img.Height > (this.Height))
            {
                factor = (float)img.Width / this.Width > img.Height / (this.Height) ? (float)img.Width / this.Width : (float)img.Height / (this.Height);
            }
            else if (img.Width < this.Width && img.Height < (this.Height))
            {
                factor = (float)img.Width / this.Width < img.Height / (this.Height) ? (float)img.Width / this.Width : (float)img.Height / (this.Height);
            }
            else if (img.Width > this.Width)
            {
                factor = (float)img.Width / this.Width;
            }
            else if (img.Height > this.Height)
            {
                factor = (float)img.Height / (this.Height);
            }
            else
            {
                factor = 1;
            }
            factor = 1 / factor;
            this.pictureBox1.Height = (int)(img.Height * factor);
            this.pictureBox1.Width = (int)(img.Width * factor);
        }

        public void UpdatePcbLocation()
        {
            this.pictureBox1.Location = new Point((this.Width / 2) - (this.pictureBox1.Width / 2), ((this.Height) / 2) - (this.pictureBox1.Height / 2));
            if (this.pictureBox1.Location.X < 0 && this.pictureBox1.Location.Y < 0)
            {
                this.pictureBox1.Location = new Point(0, 0);
            }
            else if (this.pictureBox1.Location.X < 0)
            {
                this.pictureBox1.Location = new Point(0, this.pictureBox1.Location.Y);
            }
            else if (this.pictureBox1.Location.Y < 0)
            {
                this.pictureBox1.Location = new Point(this.pictureBox1.Location.X, 0);
            }
        }

        private void SlideShowForm_SizeChanged(object sender, EventArgs e)
        {
            tableLayoutPanel1.Location = new Point((this.Width / 2) - (this.tableLayoutPanel1.Width / 2), this.Height - this.tableLayoutPanel1.Height);
            this.tableLayoutPanel1.Size = new Size(this.Width+5, this.tableLayoutPanel1.Height);
        }

        private void OptionsDown()
        {
            while (this.tableLayoutPanel1.Location.Y <= this.Height)
            {
                this.tableLayoutPanel1.Location = new Point(this.tableLayoutPanel1.Location.X, this.tableLayoutPanel1.Location.Y + 1);
                Cursor.Hide();
            }
        }

        private void OptionsUP()
        {
            while (this.tableLayoutPanel1.Location.Y > this.Height - this.tableLayoutPanel1.Height)
            {
                this.tableLayoutPanel1.Location = new Point(this.tableLayoutPanel1.Location.X, this.tableLayoutPanel1.Location.Y - 1);
                Cursor.Show();
            }
        }

        private void Pcb_Next_MouseEnter(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            pb.Image = Image.FromFile(@"Icons\" + pb.Name.Substring(4) + "Over.png");
        }

        private void Pcb_Next_MouseHover(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            pb.Image = Image.FromFile(@"Icons\" + pb.Name.Substring(4) + ".png");
        }

        private void Pcb_Next_Click(object sender, EventArgs e)
        {
            nextImage();
        }

        private void Pcb_Previous_Click(object sender, EventArgs e)
        {
            prevImage();
        }

        private void SlideShowForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.X != x && e.Y != y)
            {
                OptionsUP();
                idle = 0;
                x = e.X;
                y = e.Y;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            idle++;
            if (idle > 2)
            {
                OptionsDown();
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.nextImage();
        }

        private void SlideShowForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Escape)
            {
                this.Close();
            }
            else if (e.KeyChar == 'o' || e.KeyChar == 'O')
            {
                this.SlideShowOptions();
            }
        }

        private void SlideShowOptions()
        {
            options = new SlideShowOptions(effect, theme, speed,this.bg, this.Pcb_BackGround.BackColor);
            options.FormClosing += new FormClosingEventHandler(options_FormClosing);
            this.AddOwnedForm(options);
            options.Show();
            this.Enabled = false;
            this.timer1.Enabled = false;
            this.timer1.Stop();
            this.timer2.Enabled = false;
            this.timer2.Stop();
            this.Opacity = 0.9;
        }

        private void Pcb_SlideShowExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void options_FormClosing(object sender, EventArgs e)
        {
            this.Enabled = true;
            this.Opacity = 1;
            this.timer1.Enabled = true;
            this.timer1.Start();
            this.timer2.Enabled = true;
            this.timer2.Start();
            this.speed = options.Speed;
            this.effect = options.Effect;
            this.theme = options.Theme;
            this.bg = options.Bg;

            if (options.BgPath != null || options.BgClr != Color.FromArgb(1, 1, 1))
            {
                if (options.BgPath == null)
                {
                    this.Pcb_BackGround.Image = null;
                }
                else
                {
                    this.Pcb_BackGround.Image = Image.FromFile(options.BgPath);
                }
                this.Pcb_BackGround.BackColor = options.BgClr;
            }

            this.UpdateSpeed();
            this.nextImage();

        }

        private void UpdateSpeed()
        {
            if (speed == SlideShowSpeed.Slow)
            {
                timer1.Interval = 8000;
            }
            else if (speed == SlideShowSpeed.Medium)
            {
                timer1.Interval = 5000;
            }
            else
            {
                timer1.Interval = 3000;
            }
        }

        private void SlideShowForm_Load(object sender, EventArgs e)
        {
            this.Pcb_BackGround.BackColor = Color.FromArgb(1, 1, 1);
            if (this.slidetype == SlideShowType.FromFolder)
            {
                showImage(folderFile[selected]);
            }
            else
            {
                showImage();
            }
        }

        private void SlideShowForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Cursor.Show();
            if (slidetype == SlideShowType.FromCurrentImages)
            {
                Main.Instance.MainTabContainer.tabs[Main.CurrImage].SetActive();
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            this.SlideShowOptions();
        }

        private void Pcb_SlideShowOpen_Click(object sender, EventArgs e)
        {
            Main.Instance.OpenImage(folderFile[selected]);
        }
    }
}
